package com.ccsu.mymarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ccsu.mymarket.controller.utils.DataUtils;
import com.ccsu.mymarket.entity.Book;
import com.ccsu.mymarket.entity.message.Meta;
import com.ccsu.mymarket.entity.message.Result;
import com.ccsu.mymarket.jwt.UserLoginToken;
import com.ccsu.mymarket.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/books")
public class BookController {
    private final BookService bookService;

    public BookController(@Autowired BookService bookService) {
        this.bookService = bookService;
    }

    @UserLoginToken
    @GetMapping("/page/{pagenum}/{pagesize}")
//    获得所有图书，起始条数/限制长度
    public Result getBooks(@PathVariable Integer pagenum, @PathVariable Integer pagesize) {
        List<Book> bookList = bookService.getPageBooks(pagenum, pagesize);
        if (bookList == null)
            return new Result(new Meta(201, "暂无数据"), null);
        return new Result(new Meta(200, "查询成功！"), bookList);
    }

    @UserLoginToken
    @PostMapping("/add")
//    添加，以 Book 实体类传入所有必需数据，上架时间除外
    public Result addBook(@RequestBody Book book) {
        if (book == null || book.getClassification() == null)
            return new Result(new Meta(403, "未接收到新增数据！"), null);
        book.setInitTime(DataUtils.getDateTime(2));
        if (bookService.save(book))
            return new Result(new Meta(200, "创建商品成功！"), null);
        return new Result(new Meta(500, "操作失败！"), null);
    }

    @PostMapping("/search")
// 传入 pagenum起始条数 ，pagesize 限制条数，key搜索时用户输入的关键字
    public Result searchBook(@RequestBody Map data) {
        List<Book> bookList = bookService.searchByKey(
                (Integer) data.get("pagenum"),(Integer) data.get("pagesize"),
                (String) data.get("key"));
        if (bookList == null)
            return new Result(new Meta(201, "暂无数据"), null);
        return new Result(new Meta(200, "查询成功！"), bookList);
    }

    @UserLoginToken
    @DeleteMapping("/delete/{id}")
//    输入商品id
    public Result deleteBook(@PathVariable Integer id) {
        if (id == null)
            return new Result(new Meta(403, "请输入删除的商品id！"), null);
        if (bookService.removeById(id))
            return new Result(new Meta(200, "商品删除成功！"), null);
        return new Result(new Meta(500, "操作失败！"), null);
    }

    @UserLoginToken
    @PutMapping("/update")
//    修改图书，按Book 格式传入所有信息，上架时间除外
    public Result updateBook(@RequestBody Book book) {
        if (book == null)
            return new Result(new Meta(403, "请输入更新数据！"), null);
        if (book.getInitTime()==null){
            book.setInitTime(bookService.getOne(new LambdaQueryWrapper<Book>()
                    .eq(Book::getId,book.getId()).select(Book::getInitTime)).getInitTime());
        }
        if (bookService.updateById(book))
            return new Result(new Meta(200, "商品更新成功！"), null);
        return new Result(new Meta(500, "操作失败！"), null);
    }

    @GetMapping("/search/catagory")
//    根据三级分类名字查询图书
    public Result getPageBooksByClass(@RequestBody Map data) {
        Integer pagenum= (Integer) data.get("pagenum");
        Integer pagesize= (Integer) data.get("pagesize");
        String catagName= (String) data.get("catagName");

        List<Book> bookList = bookService.getPageBooksByClass(catagName,pagenum,pagesize);

        if (bookList == null)
            return new Result(new Meta(201, "暂无数据"), null);
        return new Result(new Meta(200, "查询成功！"), bookList);
    }

    @GetMapping("/shop/{id}")
//    根据店铺id查询图书
    public Result getPageBooksByShop(@PathVariable Integer id) {
        List<Book> bookList = bookService.getPageBooksByShop(id);
        if (bookList == null)
            return new Result(new Meta(201, "暂无数据"), null);
        return new Result(new Meta(200, "查询成功！"), bookList);
    }
}
